<!DOCTYPE html>
<html lang="en" xml:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>预约批次管理</title>
    <link rel="stylesheet" href="/layui/css/layui.css">
    <link rel="stylesheet" href="/css/custom.form.css">
</head>
<body>
<!--数据与分页-->
<div class="user_table_div">
    <!--多条件查询-->
    <div id="searchParam">
        <div class="layui-form-item">
            <div class="layui-input-inline">
                <input type="text" id="name" class="layui-input" autocomplete="off">
            </div>
            <div class="layui-input-inline ">
                <button class="layui-btn" id="search">查询</button>
            </div>
        </div>
    </div>
    <!--显示数据的表格-->
    <table class="layui-hide" id="user_table" lay-filter="user_table"></table>
    <!--分页相关 View-->
    <div id="laypage" class="layui-laypage-btn"></div>
</div>


<!-- 新增编辑公告View-->
<div class="panel panel-default operation_user" hidden>
    <div class="panel-heading title"></div>
    <div class="layui-card-body">
        <form class="layui-form " action="" lay-filter="deptInfo" style="width: 700px;margin-top: 10px">
            <input name="id" hidden/>
            <div class="layui-form-item">
                <label class="layui-form-label">标题</label>
                <div class="layui-input-block">
                    <input type="text" name="title" autocomplete="off"
                           class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">内容</label>
                <div class="layui-input-block">
                    <textarea name="content" id="demo" style="display: none;"></textarea>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-form-label ">创建时间</div>
                <div class="layui-input-block">
                    <input type="datetime-local" name="date"  autocomplete="off"
                           class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button type="submit" class="layui-btn" lay-submit="" lay-filter="submit">保存
                    </button>
                    <button class="layui-btn layui-btn-primary" id="btn_cancel">返回</button>
                </div>
            </div>
        </form>
    </div>
</div>

</body>
<script src="/layui/layui.js"></script>
<script src="/js/core.util.js"></script>
<!--新增公告按钮、批量删除 按钮-->
<script type="text/html" id="toolbar">
    <div class="layui-btn-group">
        <!--shiro:hasPermission="sys:user:add"-->
        <button type="button" class="layui-btn" lay-event="addNewUser">
            <i class="layui-icon">&#xe608;</i> 新增公告
        </button>
    </div>
    <div class="layui-btn-group">
        <!--shiro:hasPermission="sys:user:delete"-->
        <button type="button" class="layui-btn" lay-event="batchDeleted">
            <i class="layui-icon">&#xe608;</i> 批量删除
        </button>
    </div>
</script>
<!--给公告赋予角色按钮-->
<!--shiro:hasPermission="sys:user:role:update"  shiro:hasPermission="sys:user:update" shiro:hasPermission="sys:user:delete" -->
<script type="text/html" id="tool">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<script>

    layui.use(['jquery', 'form', 'table', 'laypage', 'layer', 'laydate', 'tree', 'transfer', 'layedit'], function () {
        var $ = layui.jquery;
        var form = layui.form;
        var table = layui.table;
        var laypage = layui.laypage
        var layer = layui.layer;
        var laydate = layui.laydate;
        var layedit = layui.layedit;
        /*分页参数*/
        var searchParam = {
            pageNum: 1,
            pageSize: 10,
            /*以下为多条件查询参数*/
            name: null
        }
        layedit.set({
            uploadImage: {
                url: '/file/uploadFile'
                , type: 'post'
            }
        });
        var build = layedit.build('demo', {
            height: 300,
        }); //建立编辑器

        /*动态获取数据*/
        var reloadTreeTable = function () {
            CoreUtil.sendAjax("/dormitory/ad/list", searchParam, function (res) {
                //初始化分页器
                laypageTable(res.page.totalCount, searchParam.pageNum);
                //初始化渲染数据
                loadTable(res.page.list);
                if (res.page.list != null) {
                    loadTable(res.page.list);
                }
            }, "GET", false, function (res) {
                //公告列表接口无权限回调
                layer.msg("抱歉！您暂无获取公告列表的权限");
                var noAuthorityData = [];
                loadTable(noAuthorityData);
            });
        }
        //渲染分页插件
        var laypageTable = function (count, currentPage) {
            laypage.render({
                elem: 'laypage'
                , count: count
                , limit: searchParam.pageSize
                , layout: ['count', 'prev', 'page', 'next', 'limit', 'refresh', 'skip']
                , curr: location.hash.replace('#!currentPage=', '')     //获取起始页
                , hash: 'currentPage'                                   //自定义hash值
                , jump: function (obj, first) {
                    if (!first) {
                        searchParam.pageNum = obj.curr;
                        searchParam.pageSize = obj.limit;
                        CoreUtil.sendAjax("/dormitory/ad/list", searchParam, function (res) {
                            if (res.page.list != null) {
                                loadTable(res.page.list);
                                laypageTable(res.page.totalCount, searchParam.pageNum);
                            }
                        }, "GET", false, function (res) {
                            //公告列表接口无权限回调
                            layer.msg("抱歉！您暂无获取公告列表的权限");
                            var noAuthorityData = [];
                            loadTable(noAuthorityData);
                        });
                    }
                }
            });
        };
        //渲染table
        var loadTable = function (data) {
            //展示已知数据
            table.render({
                elem: '#user_table'
                // 这个 data 为数组数据，直接 放到 data 上即可。
                , data: data
                , even: true
                , limit: data.length
                , limits: [10, 20, 30, 40, 50]
                , toolbar: '#toolbar'
                , cols: [
                    [
                        {type: 'checkbox', fixed: 'left'},
                        {field: 'id', title: 'ID', width: 300, sort: true},
                        {field: 'title', title: '标题', width: 230},
                        {
                            field: 'date', title: '创建时间', minWidth: 120, templet: function
                                (item) {
                                return CoreUtil.formattime(item.date);
                            }
                        },
                        {width: 300, toolbar: "#tool", title: '操作'}
                    ]
                ]
            });
        };
        reloadTreeTable();

        //点击新增事件， 表头工具栏事件
        table.on('toolbar(user_table)', function (obj) {
            switch (obj.event) {
                case 'addNewUser':
                    $(".title").html("新增公告");
                    $(".user_table_div").hide();
                    $(".operation_user").show();
                    $(".operation_user input[name=id]").val("");
                    $(".operation_user input[name=name]").val("");
                    $(".operation_user input[name=level]").val("");
                    $(".operation_user input[name=grade]").val("");
                    $(".operation_user input[name=organizer]").val("");
                    $(".operation_user input[name=awardDate]").val("");
                    $(".operation_user input[name=teacherId]").val("");
                    $(".operation_user input[name=category]").val("");
                    $(".operation_user input[name=note]").val("");
                    form.render(); //更新全部
                    break;
                case 'batchDeleted':
                    var checkStatus = table.checkStatus(obj.config.id);
                    var data = checkStatus.data;
                    if (data.length === 0) {
                        layer.msg("请选择要批量删除的学生");
                    } else {
                        var userIds = [];
                        $(data).each(function (index, item) {
                            userIds.push(item.id);
                        });
                        tipDialog(userIds, "选中的");
                    }
                    break;
            }
            ;
        });

        //编辑或新增的界面取消事件
        $("#btn_cancel").click(function () {
            //显示表格
            $(".user_table_div").show();
            //隐藏编辑窗口
            $(".operation_user").hide();
            //可以防止在表单事件操作后自动刷新
            return false;
        });

        //监听提交 新增和编辑  保存按钮
        form.on('submit(submit)', function (data) {
            data.field.content = layedit.getContent(build)
            //新增
            if (data.field.id === undefined || data.field.id === null || data.field.id === "") {
                CoreUtil.sendAjax("/dormitory/ad/save", JSON.stringify(data.field), function (res) {
                    //显示表格
                    reloadTreeTable(); // 自己添加
                    $(".user_table_div").show();
                    //隐藏编辑窗口
                    $(".operation_user").hide();
                    // 刷新页面，但是不管事儿
                    $(".layui-laypage-btn").click();
                }, "POST", false, function (res) {
                    //新增公告无权限提示
                    layer.msg("抱歉！您暂无新增公告的权限");
                });
                // 编辑
            } else {
                CoreUtil.sendAjax("/dormitory/ad/update", JSON.stringify(data.field), function (res) {
                    //显示表格
                    $(".user_table_div").show();
                    //隐藏编辑窗口
                    $(".operation_user").hide();
                    $(".layui-laypage-btn").click();
                }, "PUT", false, function (res) {
                    //更新公告无权限提示
                    layer.msg("抱歉！您暂无编辑公告的权限");
                });
            }
            return false;
        });


        //执行查询
        $("#search").click(function () {
            searchParam.name = $("#name").val();
            searchParam.pageNum = 1;
            CoreUtil.sendAjax("/dormitory/ad/list", searchParam, function (res) {
                if (res.page.list != null) {
                    loadTable(res.page.list);
                    laypageTable(res.page.totalCount, searchParam.pageNum);
                }
            }, "GET", false, function (res) {
                //公告列表接口无权限回调
                layer.msg("抱歉！您暂无获取公告列表的权限");
                var noAuthorityData;
                noAuthorityData = [];
                loadTable(noAuthorityData);
            });
        });


        // 操作工具栏 点击赋予角色事件事件
        table.on('tool(user_table)', function (obj) {
            var data = obj.data;
            switch (obj.event) {
                case 'edit':
                    $(".user_table_div").hide();
                    $(".operation_user").show();
                    $(".title").html("编辑学生");
                    $(".operation_user input[name=id]").val(data.id);
                    $(".operation_user input[name=name]").val(data.name);
                    $(".operation_user input[name=level]").val(data.level);
                    $(".operation_user input[name=grade]").val(data.grade);
                    $(".operation_user input[name=organizer]").val(data.organizer);
                    $(".operation_user input[name=awardDate]").val(CoreUtil.formattime(data.awardDate));
                    $(".operation_user select[name=teacherId]").val(data.teacherId);
                    $(".operation_user input[name=category]").val(data.category);
                    $(".operation_user input[name=note]").val(data.note);
                    form.render(); //更新全部
                    break;
                case 'del':
                    var userIds = [];
                    userIds.push(data.id);
                    tipDialog(userIds, data.name);
                    break;
            }
        });

        //删除前确认对话框
        var tipDialog = function (userIds, name) {
            layer.open({
                content: '确定要删除么?',
                yes: function (index, layero) {
                    layer.close(index); //如果设定了yes回调，需进行手工关闭
                    CoreUtil.sendAjax("/dormitory/ad/delete", JSON.stringify(userIds), function (res) {
                        layer.msg(res.msg);
                        $(".layui-laypage-btn").click();
                    }, "DELETE", false, function (res) {
                        //删除公告无权限
                        layer.msg("抱歉！您暂无删除公告的权限");
                    });
                }
            });
        };

        // 渲染时间
        laydate.render({
            elem: '.createTime'
            , position: 'static'
            , change: function (value, date) { //监听日期被切换
                lay('#testView').html(value);
            }
        });
    });
</script>
</html>

